package com.wd.mine.adapter

import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.wd.common.bean.NearbyChargeStationBean
import com.wd.mine.R
import com.wd.mine.callBack.OnItemClick
import java.io.File

/**
 * Created by Android Studio.
 * author: 张文龙
 * Date:
 * Time: 2023/9/15 20:34
 * 备注：
 */
class ChargeStationBeanAdapter(
    private var context: Context,
    private var list: ArrayList<NearbyChargeStationBean.Result>,
    private var callBack: OnItemClick
) : RecyclerView.Adapter<ChargeStationBeanAdapter.Holder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val inflate = LayoutInflater.from(context).inflate(R.layout.car_refueling_charging, parent, false)
        return Holder(inflate)
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val result = list[position]
        Glide.with(context)
            .load(result.imageUrl)
            .placeholder(R.mipmap.ic_launcher_round)
            .error(R.mipmap.ic_launcher_round)
            .into(holder.wattage_img)
        holder.wattage.text = result.wattage.toString()+"w"
        holder.wattage_price.text = "￥："+result.price
        holder.wattage_name.text = result.name
        holder.itemView.setOnClickListener{
            callBack.onClick(result.location!!,result.name!!)
        }
        holder.to_aMap.setOnClickListener{
            if (isInstallByread("com.autonavi.minimap")) {
                callBack.toAMap(result.name!!,result.location!!)
            }else{
                ToastUtils.showShort("请下载高德地图")
            }
        }
    }
    fun   isInstallByread(packageName : String) : Boolean{
        /**
         * 判断 API版本是否大于 29（Android 11）
         * 大于29会强制分区
         */
        return if (Build.VERSION.SDK_INT < 24) {
            File("/data/data/" + packageName).exists();
        }else {
            File("storage/emulated/0/Android/data/" + packageName).exists();
        }

    }
    class Holder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val wattage_img : ImageView
        val to_aMap : ImageView
        val wattage_name : TextView
        val wattage : TextView
        val wattage_price : TextView
        init {
            wattage_img = itemView.findViewById(R.id.wattage_img)
            to_aMap = itemView.findViewById(R.id.to_amap)
            wattage_name = itemView.findViewById(R.id.wattage_name)
            wattage = itemView.findViewById(R.id.wattage)
            wattage_price = itemView.findViewById(R.id.wattage_price)
        }

    }
}